#!/bin/sh
/bin/busybox --install -s
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
mdev -s
get_opt() {
  echo "$@" | cut -d "=" -f 2
}
init="/bin/init"
boot="/dev/sda1"
sqsh_root="/root.sqsh.img"
for i in $(cat /proc/cmdline); do
  case $i in
    boot\=*)
      boot=$(get_opt $i)
      ;;
    init\=*)
      init=$(get_opt $i)
      ;;
    sqsh_root\=*)
      sqsh_root=$(get_opt $i)
  esac
done
for i in $(seq 10); do
  [ -e "$boot" ] && break
  sleep 1
  mdev -s
done

fsck.fat -a "${boot}"
mount -o ro,umask=077 "${boot}" /boot
mount -o loop -t squashfs "/boot/${sqsh_root}" /newroot
fsck -A -T -C -p
mount -a
if [ -x "/newroot/${init}" ] ; then
  mount --move /boot /newroot/boot
  mount --move /dev /newroot/dev
  umount -f /sys /proc
  exec switch_root /newroot "${init}"
fi
echo "Failed to switch_root, dropping to a shell"
exec sh
